package com.sourcethought.xawz;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.mobclick.android.MobclickAgent;

public class SearchActivity extends Activity {
	private WebView webView;
	private String resultHtml;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
		webView = (WebView) findViewById(R.id.contentWebView);
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(Boolean.TRUE);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(Boolean.TRUE);

		AsyncTask<Integer, Integer, Integer> task = new AsyncTask<Integer, Integer, Integer>() {
			ProgressDialog progressDialog;

			@Override
			protected Integer doInBackground(Integer... params) {
				String car_type = getIntent().getStringExtra("car_type");
				String search_type = getIntent().getStringExtra("search_type");
				String car_nbr = getIntent().getStringExtra("car_nbr");
				String note_text = getIntent().getStringExtra("note_text");
				String urlString = "";
				try {
					urlString = "http://117.36.53.122:9082/wfcx/query.do?actiontype=vioSurveil&hpzl="
							+ car_type
							+ "&hphm="
							+ car_nbr
							+ "&tj="
							+ search_type
							+ "&tj_val="
							+ URLEncoder.encode(note_text, "utf-8");
					resultHtml = getDocumentAt(urlString);
				} catch (Exception e) {
					return 1;
				}
				return 0;
			}

			@Override
			protected void onPostExecute(Integer result) {
				progressDialog.dismiss();
				if (result != 0) {
					Toast.makeText(SearchActivity.this, "查询失败，请检查您的网络连接或稍后重试",
							Toast.LENGTH_LONG).show();
					return;
				}

				String tempHTML = "";
				String begin = "<table width=\"95%\"";
				String end = "<td colspan=\"6\">";
				tempHTML = begin + resultHtml.split(begin)[1];
				tempHTML = "<html><body>"
						+ tempHTML.split(end)[0]
						+ end
						+ "</td></tr><tr><td align=\"center\">本次查询结果仅供参考，以交通管理部门违法处理窗口查询结果为准。</td></tr></table></html></body>";

				webView.loadDataWithBaseURL("", tempHTML, "text/html", "utf-8",
						"");
			}

			@Override
			protected void onPreExecute() {
				progressDialog = new ProgressDialog(SearchActivity.this);
				progressDialog.setMessage("正在查询,请稍候...");
				progressDialog.show();
			}

		};
		task.execute(0);

	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	public String getDocumentAt(String urlString) throws Exception {
		StringBuilder sb = new StringBuilder();
		URL url = new URL(urlString);
		URLConnection urlConnection = url.openConnection();
		BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "GBK"));
		String line = null;
		while ((line = br.readLine()) != null) {
			sb.append(line + "\n");
		}
		return sb.toString();
	}

}